package com.jckl.foodathon.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import org.restlet.engine.util.Base64;

import com.jckl.foodathon.model.Blog;
import com.jckl.foodathon.model.Blogger;
import com.jckl.foodathon.model.Store;

public class BlogDAO {
	
	private static final Logger log = Logger.getLogger(BlogDAO.class.getName());
	
	public boolean newBlog(Blog blog){
		BloggerDAO bloggerDAO = new BloggerDAO();
		StoreDAO storeDAO = new StoreDAO();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query bloggerQuery = pm.newQuery(Blogger.class);
		Query storeQuery = pm.newQuery(Store.class);
		Transaction trx = pm.currentTransaction();
		
		boolean createdSuccess = false;
		
		try{
			trx.begin();
			
			Blogger persistedBlogger = bloggerDAO.getBloggerByName(blog.getBlogger().getName(), bloggerQuery);
			Store persistedStore = storeDAO.getStoreByName(blog.getStore().getName(), storeQuery);
			
			blog.setBlogger(persistedBlogger);
			blog.setStore(persistedStore);
			pm.makePersistent(blog);
			
			persistedBlogger.addBlog(blog);
			persistedStore.addBlog(blog);
			
			createdSuccess = true;
			trx.commit();
		}catch (Exception ex){
			ex.printStackTrace();
		}finally{
			bloggerQuery.closeAll();
			storeQuery.closeAll();
			pm.close();
		}
		return createdSuccess;
	}
	
	public List<Blog> getBlogsByLatest(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Blog.class);
		
		List<Blog> blogs = new ArrayList<Blog>();
		List<Blog> detachedBlogs = new ArrayList<Blog>();

		try{
			query.setOrdering("updatedDate desc");
			blogs = (List<Blog>) query.execute();
			
		}finally{
			pm.close();
			query.closeAll();
		}
		return detachedBlogs;
	}
	
	public List<Blog> getBlogs(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Blog.class);
		
		List<Blog> blogs = new ArrayList<Blog>();

		try{
			query.setOrdering("title asc");
			blogs = (List<Blog>) query.execute();
			
		}finally{
			pm.close();
			query.closeAll();
		}
		return blogs;
	}
	
	public Blog getBlogByURL(String brandURL){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Blog.class);
		
		List<Blog> blogs = null;
		Blog blog = null;

		try{
			query.setFilter("blogURL == blogURLLink");
			query.declareParameters("String blogURLLink");
			blogs = (List<Blog>) query.execute(brandURL);
			
			if(blogs.size()>0){
				blog = blogs.get(0);
			}
		}catch (Exception ex){
			ex.printStackTrace();
		}finally{
			pm.close();
			query.closeAll();
		}
		return blog;
	}
	
	public Blog getBlogByURL(String brandURL, Query query){
		List<Blog> blogs = null;
		Blog blog = null;

		try{
			query.setFilter("blogURL == blogURLLink");
			query.declareParameters("String blogURLLink");
			blogs = (List<Blog>) query.execute(brandURL);
			
			if(blogs.size()>0){
				blog = blogs.get(0);
			}
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return blog;
	}
	
	public boolean updateBlog (Blog blog){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		boolean updatedSuccess = false;
		
		try{
			pm.makePersistent(blog);
			updatedSuccess = true;
		}finally{
			pm.close();
		}
		return updatedSuccess;
	}
	
	public List<Blog> getBlogsByStore(String storeName){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Store.class);
		
		List<Store> stores = null;
		Store store = null;
		List<Blog> blogs = new ArrayList<Blog>();
		List<Blog> encodedBlogs = new ArrayList<Blog>();

		try{
			query.setFilter("name == storeName");
			query.declareParameters("String storeName");
			stores = (List<Store>) query.execute(storeName);
			
			if(stores.size()>0){
				store = stores.get(0);
				for (Blog blog : store.getBlogs()) {
					
					Blogger blogger = blog.getBlogger();
					blogs.add(blog);
				}
			}
			
		}catch (Exception ex){
			log.warning(ex.getMessage());
			ex.printStackTrace();
		}finally{
			pm.close();
			query.closeAll();
		}
		
		
		for (Blog blog : blogs) {
			if(blog.getBlogger().getImage() != null){
				blog.getBlogger().setEncodedImage(Base64.encode(blog.getBlogger().getImage().getBytes(), false));
				blog.getBlogger().setImage(null);
			}
			encodedBlogs.add(blog);
		}
		
		
		return encodedBlogs;
	}
}
